package com.carryking.smart.rpc.netty.code;

import com.carryking.smart.rpc.entity.ResponseEntity;
import com.carryking.smart.rpc.utils.KryoUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by jincarry on 2018/1/10.
 */
@Slf4j
public class ResponseEntityEncoder extends MessageToByteEncoder<ResponseEntity> {

    protected void encode(ChannelHandlerContext ctx, ResponseEntity msg, ByteBuf out) throws Exception {
        if (msg == null) {
            throw new RuntimeException("Encoder ResponseEntity失败,远程服务调用响应实体不能为空");
        }
        if (log.isDebugEnabled()) {
            log.debug("Encoder ResponseEntity:ResponseEntity={}", msg);
        }
        out.writeBytes(KryoUtils.wirte(msg));
    }
}
